package com.lenovo.wxtoken.controller;

import com.lenovo.wxtoken.entity.ResultBean;
import com.lenovo.wxtoken.service.OauthService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * 钉钉基础服务类
 * 当前类不捕获异常  通过aop处理
 */
@RestController
public class OAoauthController {

    @Autowired
    private OauthService oauthService;

    @ApiOperation(value = "获取token", notes = "使用参数中的企业信息")
    @ApiResponse(code = 200, message = "请求成功", response = ResultBean.class)
    @GetMapping("token")
    public ResultBean getToken(@ApiParam("企业Id") String corpId, @ApiParam("秘钥") String secret) throws Exception {

        String msg = oauthService.getAccessToken(corpId, secret);
        String expire = oauthService.getExpire();

        return new ResultBean<String>(msg, expire);
    }

    @ApiOperation(value = "获取js_ticket", notes = "使用参数中的企业信息")
    @ApiResponse(code = 200, message = "请求成功", response = ResultBean.class)
    @GetMapping("jsTicket")
    public ResultBean getJsticket(@ApiParam("企业Id") String corpId, @ApiParam("秘钥") String secret) throws Exception {
       String token = oauthService.getAccessToken(corpId, secret);
       String expire = oauthService.getExpire();
       String msg = oauthService.getJsTicket(token);
       return new ResultBean(msg, expire);
    }

    @ApiOperation(value = "获取js_config", notes = "使用参数中的企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpId", value = "企业Id"),
            @ApiImplicitParam(name = "secret", value = "秘钥"),
            @ApiImplicitParam(name = "agentId", value = "应用Id"),
            @ApiImplicitParam(name = "url", value = "需要验证的url")
    })
    @ApiResponse(code = 200, message = "请求成功", response = ResultBean.class)
    @GetMapping("jsConfig")
    public ResultBean getJsConfig(String corpId, String secret, String agentId, String url) throws Exception {
        String token = oauthService.getAccessToken(corpId, secret);
        String expire = oauthService.getExpire();
        String ticket = oauthService.getJsTicket(token);
        String result = oauthService.getConfig(corpId, agentId, ticket, url);

        return new ResultBean(result, expire);
    }

}
